﻿var app = angular.module('stuApp', ['ngRoute', 'oc.lazyLoad', 'ngResource','ngCookies']);
app.config(function ($routeProvider, $ocLazyLoadProvider, $locationProvider) {
    $locationProvider.hashPrefix('');
    $routeProvider
    //首页
        .when('/', {
            templateUrl: '/modules/student/tmpl/main.html',
            controller: 'stuCtr',
            resolve: {
						loadMyCtrl: ['$ocLazyLoad', function($ocLazyLoad) {
							// you can lazy load controllers
                            return $ocLazyLoad.load([{
                                serie: true,
                                files: ['/modules/student/controllers/student.js']
                            },
                                {
                                    serie: true,
                                    files: ['/modules/student/services.js']
                                }
                                ]);
						}]
					}
        })
        //解析
        .when('/parse', {
            templateUrl: '/modules/student/tmpl/parse.html',
            controller: 'parseCtr',
            resolve: {
                loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
                    // you can lazy load controllers
                    return $ocLazyLoad.load([{
                        serie: true,
                        files: ['/modules/student/controllers/parse.js']
                    },
                    {
                        serie: true,
                        files: ['/modules/student/services.js']
                    },
                    ]);
                }]
            }
        })
        //错题页
        .when('/error', {
            templateUrl: '/modules/student/tmpl/error.html',
            controller: 'errorCtr',
            resolve: {
                loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
                    // you can lazy load controllers
                    return $ocLazyLoad.load([{
                        serie: true,
                        files: ['/modules/student/controllers/error.js']
                    },
                    {
                        serie: true,
                        files: ['/modules/student/services.js']
                    },
                    ]);
                }]
            }
        })
        //设置
        .when('/set', {
            abstract: true,
            templateUrl: '/modules/set.html',
            controller: 'setCtr',
            resolve: {
                loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load([{
                        serie: true,
                        files: ['/modules/teacher/controllers/setCtr.js']
                    }]);
                }]
            }
        })
        //修改密码
        .when('/reg', {
            templateUrl: '/modules/register.html',
            controller: 'regCtr',
            resolve: {
                loadMyCtrl: ['$ocLazyLoad', function ($ocLazyLoad) {
                    return $ocLazyLoad.load([{
                        serie: true,
                        files: ['/modules/teacher/controllers/regCtr.js']
                    }]);
                }]
            }
        })
        

})
app.controller('topCtr', function ($rootScope,$scope, $cookieStore, mainService) {
    $scope.materialsList = [];//教材
    //注销
    $scope.logout = function () {
        $cookieStore.remove("loginInfo");
        window.location.href = '/login.html';
    };
    //cookie
    $scope.getCookie = function () {
        try {
            //获取用户信息
            $scope.userid = $cookieStore.get('loginInfo').empid;
            $scope.username = $cookieStore.get('loginInfo').name;
            var type = $cookieStore.get('loginInfo').usertype;
            if (type === 1) {
                window.location.href = "/login.html";
            }
        }
        catch (e) {
            //获取用户信息失败则返回登录页面
            window.location.href = "/login.html";
        }
        $scope.getMaterial();
    }
    //获取学生选择的教材
    $scope.Getstu_selmater = function () {
        var par = {
            "student_id": $scope.userid
        }
        mainService.Getstu_selmater(par, function (data) {
            $scope.material_id = data.data[0].material_id;
            $rootScope.material_id = $scope.material_id;
            $($scope.materialsList).each(function () {
                if (this.id == $scope.material_id) {
                    this.ischosen = 1;
                    $scope.book_type = this.book_type;
                } else {
                    this.ischosen = 0;
                }
            })
            //如果没有数据，则弹出模态框
            if ($scope.material_id == null) {
                $scope.modal();
            }
        })
    }
    //插入学生选中教材
    $scope.stu_material = function () {
        var par = {
            "student_id": $scope.userid,
            "material_id": $scope.material_id,
        }
        mainService.stu_material(par, function (data) {
            if (data) {

            }
        })
    }
    //更新学生选中教材
    $scope.stu_materialupd=function () {
        var par = {
            "student_id": $scope.userid,
            "material_id": $scope.material_id,
        }
        mainService.stu_materialupd(par, function (data) {
            if (data) {

            }
        })
    }
    //选择教材
    $scope.chooseMaterial = function (index) {
        $($scope.materialsList).each(function () {
            this.ischosen = 0;
        })
        $scope.materialsList[index].ischosen = 1;
        $("#example").modal('hide');
        //插入
        if ($scope.material_id == null) {
            $scope.material_id = $scope.materialsList[index].id;
            $scope.stu_material();
        } else { //更新
            $scope.material_id = $scope.materialsList[index].id;
            $scope.stu_materialupd();
        }
        $rootScope.material_id = $scope.material_id;
        $scope.book_type= $scope.materialsList[index].book_type;
    }
    //模态框
    $scope.modal = function () {
        $("#example").modal('show');
    }
    //教材列表
    $scope.getMaterial = function () {
        $scope.par_material = {
            "CourseName": "化学",
        };
        mainService.getMaterial($scope.par_material, function (data) {
            $scope.materialsList = data.data;
            $scope.Getstu_selmater();
        })
    }
    //加载
    $scope.init = function () {
        $scope.getCookie();
    }
    $scope.init();
})
